package xfuzzy.alg_genetico.indeterminado.xfghlOld.model;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
//import java.util.Date;
import java.util.Properties;
//import java.util.Random;
import java.util.logging.Logger;

//import org.apache.log4j.Logger;

/**
 * Clase que va a generar un objeto config leido desde un fichero de propiedades
 * 
 * @author Sergio
 * 
 */
public class XfghlGenerateConfig {

	private static Logger logger = Logger.getLogger("GenerateConfig");
	private static long propsFileLastUpdate = -1L;
	private static Properties properties = new Properties();

	private static int[] seed;

	public XfghlGenerateConfig() {
		// Auto-generated constructor stub
	}

	/**
	 * Obtiene una configuracion a partir del fichero config.properties
	 * 
	 * @return
	 */
	public static XfghlConfig getConfig() {
		// Cargamos el properties
		init();

		String nameSystem = properties.getProperty("nameSystem");
		// logger.info("nameSystem: "+nameSystem);
		int numIterAjus = Integer.parseInt(properties.getProperty("numIterAjus"));
		// logger.info("numIterAjus: "+numIterAjus);
		int numThread = Integer.parseInt(properties.getProperty("numThread"));
		// logger.info("numThread: "+numThread);
		File file = new File(properties.getProperty("file"));
		// logger.info("file: "+file);
		double iniUpd = Double.parseDouble(properties.getProperty("iniUpd"));
		// logger.info("iniUpd: "+iniUpd);
		double incFact = Double.parseDouble(properties.getProperty("incFact"));
		// logger.info("incFact: "+incFact);
		double decFact = Double.parseDouble(properties.getProperty("decFact"));
		// logger.info("decFact: "+decFact);
		int andOperator = Integer.parseInt(properties.getProperty("andOperator"));
		// logger.info("andOperator: "+andOperator);
		int defuzOperator = Integer.parseInt(properties.getProperty("defuzOperator"));
		// logger.info("defuzOperator: "+defuzOperator);
		int styleInPut = Integer.parseInt(properties.getProperty("styleInPut"));
		// logger.info("styleInPut: "+styleInPut);
		int mfs = Integer.parseInt(properties.getProperty("mfs"));
		// logger.info("mfs: "+mfs);
		int alg = Integer.parseInt(properties.getProperty("alg"));
		// logger.info("alg: "+alg);
		int numChrom = Integer.parseInt(properties.getProperty("numChrom"));
		// logger.info("numChrom: "+numChrom);
		int numGenerations = Integer.parseInt(properties.getProperty("numGenerations"));
		// logger.info("numGenerations: "+numGenerations);
		double probMut = Double.parseDouble(properties.getProperty("probMut"));
		// logger.info("probMut: "+probMut);
		double probCross = Double.parseDouble(properties.getProperty("probCross"));
		// logger.info("probCross: "+probCross);

		// Creamos el array de semillas
		String[] sSeed = properties.getProperty("seed").split(",");

		seed = new int[sSeed.length];

		for (int i = 0; i < sSeed.length; i++) {
			seed[i] = Integer.parseInt(sSeed[i]);
		}

		return new XfghlConfig(nameSystem, numIterAjus, numThread, file, iniUpd, incFact, decFact, andOperator,
				defuzOperator, styleInPut, mfs, alg, numChrom, numGenerations, probMut, probCross);
	}

	private static synchronized void init() {
		try {
			File localFile = getPropertiesResource();
			if (propsFileLastUpdate != localFile.lastModified()) {
				logger.info("Obteniendo propiedades");
				properties = new Properties();
				FileInputStream localFileInputStream = new FileInputStream(localFile);
				properties.load(localFileInputStream);
				propsFileLastUpdate = localFile.lastModified();
				// logger.debug("Propiedades obtenidas:\n" + properties);
				// logger.debug("Fecha ultima actualizacion fichero de propiedades: "
				// + new Date(propsFileLastUpdate));
			}
		} catch (URISyntaxException localURISyntaxException) {
			// logger.error("Error al obtener las propiedades de /config.properties: "
			// + localURISyntaxException.getMessage());
			// logger.error(localURISyntaxException);
			properties = new Properties();
		} catch (IOException localIOException) {
			// logger.error("Error al obtener las propiedades de /config.properties: "
			// + localIOException.getMessage());
			// logger.error(localIOException);
			properties = new Properties();
		}
	}

	private static File getPropertiesResource() throws URISyntaxException {
		URL localURL = XfghlGenerateConfig.class.getResource("/config.properties");
		if (localURL == null)
			throw new URISyntaxException(null, "No fue posible obtener el recurso /config.properties.");
		URI localURI = new URI(localURL.toString());
		File localFile = new File(localURI);
		return localFile;
	}

	public static int[] getSeed() {
		return seed;
	}
}
